const { Goods } = require('../../../model/goods');
const moment = require('moment')
const { goodsState } = require('../../../enum/state')

module.exports = async (req, res) => {
  const { id, state } = req.body
  // let body = {}
  // if (req.body.state === 1) {
  //     body.publishDate = moment().format('YYYY-MM-DD HH:mm:ss')
  // }
  console.log('修改', req.body)
  const current = await Goods.findOne({_id: id})
  const keys = Object.keys(req.body)
  // 点击下架不需要审核!(keys.length === 2 && state >= 0 )
  if (!(keys.length === 2 && state >= 0 ) && [goodsState.unsanctioned, goodsState.published, goodsState.suspensive].includes(current.state)) {
    // 编辑信息
    req.body.state = goodsState.pending
  }
  Goods.updateOne({_id: id}, {
    // state
    ...req.body
  }, (err, row) => {
    if(!err) {
      res.json({
        message: '更新成功!'
      })
    } else {
      res.status(403)
      res.json({
        log: err,
        message: '更新失败！'
      })
    }
  })
}